//
//  HttpRequestUtil.swift
//  Retail
//
//  Created by zhangjikuan on 2020/7/1.
//  Copyright © 2020 aqdeal. All rights reserved.
//

import UIKit
import HandyJSON
import Alamofire

class AjaxResult: NSObject,HandyJSON {
    @objc var my_date : Int = 0{
        willSet {
            print("newValue: ", newValue)
        }
        didSet {
            print("oldValue: ", oldValue)
        }
    }
    required override init() {
        
    }
}

class HttpRequestUtil: NSObject {
    typealias HttpSuccess = ((_ result:AjaxResult) -> Void)
    static func request( method:HTTPMethod,url:URLConvertible,parameters:Parameters? = nil,success:@escaping HttpSuccess){
        AF.request(url, method:method, parameters:parameters).responseJSON(completionHandler: { (response) in
            switch response.result {
            case .success(let json):
                print(json)
                if let obj = AjaxResult.deserialize(from: json as? Dictionary) {
                    success(obj)
                } else {
                    _ = AjaxResult()
                
                    
                }
                
                break
            case .failure(let error):
                print("error:\(error)")
                break
                
            }
        })
    }


}
